package Servlet;

import Until.BaseDao;
import Until.BaseModel;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "AddUserServlet",urlPatterns = "/adduser")
public class AddUserServlet extends HttpServlet {
    BaseDao baseDao = new BaseDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object[] params= new Object[6];
        params[0]=req.getParameter("name");
        params[1]=req.getParameter("pwd");
        params[2]=req.getParameter("telephone");
        params[3]=req.getParameter("cardId");
        params[4]=req.getParameter("email");
        params[5]=new Date();
        String sql="insert into T_user(name,trueName,telephone,cardId,email,regTime) values(?,?,?,?,?,?)";
        int result=baseDao.update(sql,params);
        BaseModel baseModel = new BaseModel();
        if (result>0){
            baseModel.setCode(0);
            baseModel.setMessage("用户新增成功！");
        }else {
            baseModel.setCode(-1);
            baseModel.setMessage("用户新增失败！");
        }
        String baseModelStr=JSON.toJSONString(baseModel);
        resp.getWriter().write(baseModelStr);
        resp.getWriter().flush();
        resp.getWriter().close();
    }
}
